﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DFXEditorApp
{
    public class MessageHandler : IMessageFilter
    {
        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;
        const int WM_LBUTTONDBLCLK = 0x0203;
        const int WM_RBUTTONDOWN = 0x0204;
        const int WM_RBUTTONUP = 0x0205;
        const int WM_RBUTTONDBLCLK = 0x0206;
        const int WM_MBUTTONDOWN = 0x0207;
        const int WM_MBUTTONUP = 0x0208;
        const int WM_MBUTTONDBLCLK = 0x0209;

        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;
        const int WM_SYSKEYDOWN = 0x0104;
        const int WM_SYSKEYUP = 0x0105;
        const int WM_CLOSE = 0x0010;

        private IntPtr formHandle;
        private IntPtr panelHandle;
        private EditorApp editorApp;

        public MessageHandler(IntPtr formHandle, IntPtr panelHandle, EditorApp editorApp)
        {
            this.formHandle = formHandle;
            this.panelHandle = panelHandle;
            this.editorApp = editorApp;
        }

        public bool PreFilterMessage(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_LBUTTONDOWN:
                case WM_LBUTTONUP:
                case WM_LBUTTONDBLCLK:
                case WM_RBUTTONDOWN:
                case WM_RBUTTONUP:
                case WM_RBUTTONDBLCLK:
                case WM_MBUTTONDOWN:
                case WM_MBUTTONUP:
                case WM_MBUTTONDBLCLK:
                case WM_KEYDOWN:
                case WM_KEYUP:
                case WM_SYSKEYDOWN:
                case WM_SYSKEYUP:
                case WM_CLOSE:
                    {
                        NativeMethods.EditorWndProc(m.HWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
                    }
                    return true;
            }            

            return false;
        }

        public void Application_Idle(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NativeMethods.EditorRender(i);
                    NativeMethods.EditorSwapBuffers(i); 
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                       
        }
    }
}
